# -*- coding:utf8 -*-
import numpy as np
import seaborn as sns
import pandas as pd

import matplotlib.pyplot as plt

csv_file = r"data/boston_housing.csv"

df = pd.read_csv(csv_file)
# 打印前5行
# print(df.head())
# 打印前3行
# print(df.head(3))
# 打印表格信息
# print(df.info())

# 形状
# print(df.shape)
# 列名
# print(df.columns)

# 数据描述信息(个数,均值,标准差,最小值,最大值,分位数)
# print(df.describe())

# 查看单个变量的分布(房价中位数的直方图分布+和密度函数分布)
# print(df.columns)
# fig = plt.figure()
# sns.distplot(df["MEDV"], bins=30, kde=True)
# plt.xlabel("Median value of owner-occupied homes", fontsize=12)
# plt.show()
#
# fig = plt.figure()
# sns.distplot(np.log1p(df["MEDV"]), bins=30, kde=True)
# plt.xlabel("Median value of owner-occupied homes", fontsize=12)
# plt.show()

# 查看单个变量的分布2(箱型图和提琴图)
# plt.figure()
# _, axes = plt.subplots(1, 2, sharey=True, figsize=(6, 4))
# sns.boxplot(data=df["MEDV"], ax=axes[0])
# sns.violinplot(data=df["MEDV"], ax=axes[1])
# plt.show()

# 查看单个变量的分布3(犯罪率CRIM的直方图分布)
# plt.figure()
# sns.distplot(df["CRIM"], bins=30, kde=True)
# plt.xlabel("CRIM data")
# plt.show()

# 查看离散型变量的条形图
# plt.figure()
# sns.countplot(df["CHAS"], order=[0, 1])
# plt.xlabel("Charles River")
# plt.ylabel("Number of occurrences")
# plt.show()

# 查看单个变量的分布(一氧化氮浓度)
# plt.figure()
# sns.distplot(df["NOX"], bins=30, kde=True)
# plt.xlabel("NOX data", fontsize=12)
# plt.ylabel("num")
# plt.show()

# 查看单个变量的分布(房间数RM)
# plt.figure()
# sns.distplot(df["RM"], bins=30, kde=True)
# plt.xlabel("Average number of rooms per dwelling", fontsize=12)
# plt.show()

# 查看单个变量的分布(房龄AGE)
# plt.figure()
# sns.distplot(df["AGE"], bins=30, kde=False)
# plt.xlabel("proportion of owner-occupied units built prior to 1940", fontsize=12)
# plt.show()

# 查看单个变量的分布(距离DIS)
# plt.figure()
# sns.distplot(df["DIS"], bins=30, kde=False)
# plt.xlabel("weighted distance to five Boston employment centers", fontsize=12)
# plt.show()

# 查看单个变量的分布(交通RAD)
# plt.figure()
# sns.countplot(df["RAD"])
# plt.xlabel("index of accessibility to radial highways")
# plt.show()

# 查看单个变量的分布(税收TAX)
# plt.figure()
# sns.distplot(df["TAX"], bins=30, kde=False)
# plt.xlabel("Full-value property-tax rate per $10,000", fontsize=12)
# plt.show()

# 查看单个变量的分布(师生比PTRATIO)
# plt.figure()
# sns.distplot(df["PTRATIO"], bins=30, kde=False)
# plt.xlabel("Pupil-teacher ratio by town")
# plt.show()

# 查看单个变量的分布(黑人比例B)
# plt.figure()
# sns.distplot(df["B"], bins=30, kde=False)
# plt.xlabel("proportion of blacks", fontsize=12)
# plt.show()

# 查看单个变量的分布(低收入人群比例LSTAT)
# plt.figure()
# sns.distplot(df["LSTAT"], bins=30, kde=False)
# plt.xlabel("lower status of the population", fontsize=12)
# plt.show()

# 查看特征之间的相关矩阵
# plt.figure()
# sns.heatmap(df.corr(), annot=True) # 相关系数大于0.5的认为是强相关
# plt.show()

# 查看相关系数超过0.5的特征对
threshold = 0.5
corr_list = []
data_corr = df.corr()
size = data_corr.shape[0]

for i in range(0, size):
    for j in range(i + 1, size):
        corr = data_corr.iloc[i, j]
        if abs(corr) >= threshold:
            corr_list.append((corr, i, j))

cols = df.columns
corr_list = sorted(corr_list, key=lambda x: -abs(x[0]))
for v, i, j in corr_list:
    print("{} and {} = {:.2f}".format(cols[i], cols[j], v))

plt.figure()
# plt.scatter(df["RM"], df["MEDV"])
# sns.jointplot(x="RM", y="MEDV", data=df, kind="scatter")
# for v, i, j in corr_list:
#     sns.pairplot(df, x_vars=cols[i], y_vars=cols[j])
sns.lmplot("RM", "MEDV", df, "CHAS", fit_reg=False) # 靠近河边，所以房价稍微贵一点，嘿嘿，橙色的点多一些
plt.show()
